home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 182_01 / addquip.c < prev    next >
Text File  |  1990-07-30  |  1KB  |  74 lines

  1. #include    <stdio.h>
  2.  
  3. FILE    *seekfp,        /* the address file */
  4.     *fortfp;        /* the actual fortune file */
  5.  
  6. #include    <quip.h>
  7.  
  8. long    ftell(),
  9.     fseek();
  10.  
  11. openfiles()
  12. {
  13.     seekfp = fopen(seekname, "r+");
  14.     if (seekfp == 0) {
  15.         puts("Cannot open address file.");
  16.         exit(0);
  17.     }
  18.     fortfp = fopen(quipname, "r+");
  19.     if (fortfp == 0) {
  20.         puts("Cannot open fortunes file.");
  21.         exit(0);
  22.     }
  23. }
  24.  
  25. putaddr(addr)
  26. long    addr;
  27. {
  28.     if (fwrite(&addr, sizeof(long), 1, seekfp) < 1)
  29.         printf("write error on address file\n");
  30. }
  31.  
  32. char    fortune[2048];
  33.  
  34. addfort()
  35. {
  36.     int    i,
  37.         c;
  38.     long    now;
  39.  
  40.     if (fseek(seekfp, 0L, 2) / 2 >= 32767L) {
  41.         puts("Sorry, the quip file has too many entries.");
  42.         return;
  43.     }
  44.     fseek(fortfp, 0L, 2);
  45.     puts("Add your quip and end with a ^D (can be a maximum of 2048 characters):");
  46.     i = 0;
  47.     while ((c = getchar()) != EOF)
  48.         fortune[i++] = c;
  49.     time(&now);
  50.     fprintf(fortfp, ". %s\n", getlogin());
  51.     putaddr(ftell(fortfp));
  52.     if (fwrite(fortune, i, 1, fortfp) != 1) {
  53.         printf("Write failed!\n");
  54.         exit(-1);
  55.     }
  56. }
  57.  
  58. closefiles()
  59. {
  60.     fclose(seekfp);
  61.     fclose(fortfp);
  62. }
  63.  
  64. main()
  65. {
  66.     if (!strcmp(getlogin(), "demo") || !strcmp(getlogin(), "intro")) {
  67.         printf("Sorry, but demo cannot add quips.\n");
  68.         exit(0);
  69.     }
  70.     openfiles();
  71.     addfort();
  72.     closefiles();
  73. }
  74.